#此扩展用于的模块扩展处理,用于支撑模块的
#   EWX_HAS_CONAN_ENV:
#       此变量是公共cmake模块查找conan环境决定的变量类型
#       目的是为了支撑conan方式和非conan方式的同时处理
#   此模块主要决定以下几个环境支撑
#   [1]查找模块
#   [2]编绎目标
#   [3]调试环境变量:
#       生成VS调试环境的变量处理
#   安装:
#       自动安装库文件到安装目录
include(support_3rd_utils)
SET(SUPPORT_3RD_PKG_NAME qt)
IF(EWX_HAS_CONAN_ENV)
   #这里通过手动适配位置 by luli
	IF(NOT qt_ROOT_DIR OR NOT EXISTS ${qt_ROOT_DIR})
      SET(qt_ROOT_DIR ${CONAN_QT_ROOT})
   ENDIF()

   #
   set(_DEFALUE_COMPONETS
            Core  
            Network Quick Gui Widgets Sql 
            Xml
    )
   
   find_package(Qt5
         COMPONENTS 
            ${_DEFALUE_COMPONETS}
         NO_SYSTEM_ENVIRONMENT_PATH
         NO_CMAKE_SYSTEM_PATH
         #REQUIRED
   )
   message(NOTICE "===Use Quit Mode Seach   Qt..Qt5_DIR=${Qt5_DIR}" )

   if (NOT Qt5_FOUND)
      SET(PROJECT_QTDIR "" CACHE PATH "Select Qt Root PATH")
      IF(NOT EXISTS ${PROJECT_QTDIR})
         message(FATAL_ERROR "Please set PROJECT_QTDIR to the root of your Qt5 installation.")
      ENDIF()

      find_package(Qt5 REQUIRED CONFIG 
         COMPONENTS ${_DEFALUE_COMPONETS} 
         REQUIRED
         NO_DEFAULT_PATH 
         PATHS 
            ${PROJECT_QTDIR}
      )
   else()
      message(NOTICE "${SUPPORT_3RD_PKG_NAME} Use Conan Env Mode.... 
         PKG [${SUPPORT_3RD_PKG_NAME}] Root At ${CONAN_QT_ROOT}")
      SET(PROJECT_QTDIR ${CONAN_QT_ROOT})
   endif()


    SET(Qt5_FOUND TRUE)

   if(WIN32)
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_RELEASE 
               ${CONAN_QT_ROOT}/bin)
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_DEBUG 
               ${CONAN_QT_ROOT}/bin)
   else()
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_RELEASE 
               ${CONAN_QT_ROOT}/bin)
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_DEBUG 
               ${CONAN_QT_ROOT}/bin)
   endif()
ELSE()
   message(NOTICE "${SUPPORT_3RD_PKG_NAME} Use Normal Package Mode....")

   #这里通过手动适配位置 by luli
	IF(NOT qt_ROOT_DIR OR NOT EXISTS ${qt_ROOT_DIR})
      IF(WIN32)
         SEARCH_3RD_MAYBE_PATH(qt_ROOT_DIR 
            ${PROJECT_3RDPARTY_DIR}/qt-5.12.9-x64-vs2017
            ${PROJECT_3RDPARTY_DIR}/qt-5.12.9
         )
      ELSE()
         GET_THIRD_PARTY_PACKAGE_DIR_NAME("qt-5.15.4" full_package_name1)
         GET_THIRD_PARTY_PACKAGE_DIR_NAME("qt-5.15.14" full_package_name2)
         GET_THIRD_PARTY_PACKAGE_DIR_NAME("qt-5.12.9" full_package_name3)

         message(NOTICE ">>>>Ready package: ${full_package_name1}")
         
         SEARCH_3RD_MAYBE_PATH2(${SUPPORT_3RD_PKG_NAME} qt_ROOT_DIR 
            ${PROJECT_3RDPARTY_DIR}/${full_package_name1}
            ${PROJECT_3RDPARTY_DIR}/${full_package_name2}
            ${PROJECT_3RDPARTY_DIR}/${full_package_name3}
         )
      ENDIF()
   ENDIF()

   SET(PROJECT_QTDIR ${qt_ROOT_DIR})

   SET(Qt5Charts_DIR ${qt_ROOT_DIR}/lib/cmake/Qt5Charts)
   SET(Qt5WinExtras_DIR ${qt_ROOT_DIR}/lib/cmake/Qt5WinExtras)
   SET(Qt5Svg_DIR ${qt_ROOT_DIR}/lib/cmake/Qt5Svg)

   find_package(Qt5
        COMPONENTS 
            Core 
            Network 
            Sql 
            Xml 
            Concurrent 
            Widgets
            OpenGL
            PrintSupport
        REQUIRED 
        CONFIG
        NO_DEFAULT_PATH PATHS ${PROJECT_QTDIR}
    )
    SET(Qt5_FOUND TRUE)

   if(WIN32)
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_RELEASE 
               ${qt_ROOT_DIR}/bin)
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_DEBUG 
               ${qt_ROOT_DIR}/bin)
   else()
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_RELEASE 
               ${qt_ROOT_DIR}/bin)
      set_property(GLOBAL PROPERTY ${SUPPORT_3RD_PKG_NAME}_BINDIR_DEBUG 
               ${qt_ROOT_DIR}/bin)
   endif()
ENDIF()

#=====================================================
# 默认插件安装的目录 bin/<QT_PLUGIN_INSTALL_DIR>
SET(QT_PLUGIN_INSTALL_DIR "qt_plugins" CACHE STRING 
   "Default qt plugins Install Dir. Relatetive in Bin Folder." FORCE)
set(QTROOT ${qt_ROOT_DIR})

list(APPEND CMAKE_PREFIX_PATH ${QTROOT})
list(REMOVE_DUPLICATES CMAKE_PREFIX_PATH)

set(QTPLUGINPATH "${QTROOT}/plugins")

# ==========================================
# 增加依赖安装处理，独立处理的qt才有此文件夹
if(EXISTS ${QTROOT}/deps_libs)
   install(DIRECTORY "${QTROOT}/deps_libs" 
      DESTINATION ${CMAKE_INSTALL_PREFIX}
      COMPONENT Runtime)
endif()
# ==========================================

# Finds the subdirectories of a given directory.
macro(subdir_list result curdir)
   file(GLOB children RELATIVE ${curdir} ${curdir}/*)
   set(dirlist "")
   foreach(child ${children})
      if(IS_DIRECTORY ${curdir}/${child})
         set(dirlist ${dirlist} ${child})
      endif()
   endforeach()
   set(${result} ${dirlist})
endmacro(subdir_list result curdir)

# Installs each folder under Qt "plugins" to the bin folder.
macro(install_qt_platform_dlls LIBROOT)
   subdir_list(PLUGINS_DIRS ${LIBROOT}/plugins)
   foreach (dir ${PLUGINS_DIRS})
      install(DIRECTORY "${LIBROOT}/plugins/${dir}" DESTINATION ${INSTALL_DLL_PATH} COMPONENT Runtime)
   endforeach()
endmacro(install_qt_platform_dlls LIBROOT)

# install a single qt lib
macro(install_qt_lib LIBNAME)
   if(WIN32)
      install(FILES ${QTROOT}/bin/${LIBNAME}.dll DESTINATION "${INSTALL_DLL_PATH}" CONFIGURATIONS Release RelWithDebInfo COMPONENT Runtime)
      install(FILES ${QTROOT}/bin/${LIBNAME}d.dll DESTINATION "${INSTALL_DLL_PATH}" CONFIGURATIONS Debug COMPONENT Runtime)
   else()
      file(GLOB qt_SOS1 ${QTROOT}/lib/lib${LIBNAME}*.so)
      file(GLOB qt_SOS2 ${QTROOT}/lib/lib${LIBNAME}*.so*)
      install(FILES
         ${qt_SOS1} ${qt_SOS2}
         DESTINATION bin COMPONENT Runtime)
   endif()
endmacro()

macro(install_qt_core_libs)
   install_qt_lib(Qt5Core)
endmacro()

# Install a set of qt libs.  This command would install all libs:
#   install_qt_libs(Qt5Concurrent Qt5Core Qt5Gui Qt5Network Qt5OpenGL Qt5PrintSupport Qt5Script Qt5Widgets Qt5DBus)
macro(install_qt_libs)
   foreach (LIBLIST ${ARGN})
      foreach (LIB ${LIBLIST})
         install_qt_lib(${LIB})
      endforeach()
   endforeach()
   if(NOT WIN32)
      # needed for linux
      install_qt_lib(Qt5DBus)
      install_qt_lib(Qt5XcbQpa)
   endif()
endmacro()

macro(install_qt_libs_all)
   install_qt_core_libs()
   install_qt_libs(Qt5Concurrent Qt5Core Qt5Sql Qt5Gui Qt5Network Qt5OpenGL Qt5PrintSupport Qt5Script Qt5Widgets)
endmacro()

macro(install_qt_plugins)
   SET(OUTDIR ${CMAKE_INSTALL_PREFIX}/bin/${QT_PLUGIN_INSTALL_DIR})

   if(WIN32)
      set(FILESUFFIX .dll)
      set(FILEPREFIX)
      set(DBGSUFFIX d)
   else()
      set(FILESUFFIX .so)
      set(FILEPREFIX lib)
      set(DBGSUFFIX)
   endif()

   # Copy plugins
   foreach(DLL qwindowsvistastyle)
      if(EXISTS ${QTROOT}/plugins/styles/${FILEPREFIX}${DLL}${FILESUFFIX})
         if(WIN32)
            install(FILES ${QTROOT}/plugins/styles/${FILEPREFIX}${DLL}d${FILESUFFIX} DESTINATION "${OUTDIR}/styles" CONFIGURATIONS Debug COMPONENT Runtime)
            install(FILES ${QTROOT}/plugins/styles/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/styles" CONFIGURATIONS Release RelWithDebInfo COMPONENT Runtime)
         else()
            install(FILES ${QTROOT}/plugins/styles/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/styles" COMPONENT Runtime)
         endif()
      endif()
   endforeach()
   foreach(DLL qsqlite;qsqlodbc)
      if(EXISTS ${QTROOT}/plugins/sqldrivers/${FILEPREFIX}${DLL}${FILESUFFIX})
         if(WIN32)
            install(FILES ${QTROOT}/plugins/sqldrivers/${FILEPREFIX}${DLL}d${FILESUFFIX} DESTINATION "${OUTDIR}/sqldrivers" CONFIGURATIONS Debug COMPONENT Runtime)
            install(FILES ${QTROOT}/plugins/sqldrivers/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/sqldrivers" CONFIGURATIONS Release RelWithDebInfo COMPONENT Runtime)
         else()
            install(FILES ${QTROOT}/plugins/sqldrivers/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/sqldrivers" COMPONENT Runtime)
         endif()
      endif()
   endforeach()

   foreach(DLL qgif;qico;qjpeg;qtga;qtiff;qwbmp;qsvg)
      if(EXISTS ${QTROOT}/plugins/imageformats/${FILEPREFIX}${DLL}${FILESUFFIX})
         if(WIN32)
            install(FILES ${QTROOT}/plugins/imageformats/${FILEPREFIX}${DLL}d${FILESUFFIX} DESTINATION "${OUTDIR}/imageformats" CONFIGURATIONS Debug COMPONENT Runtime)
            install(FILES ${QTROOT}/plugins/imageformats/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/imageformats" CONFIGURATIONS Release RelWithDebInfo COMPONENT Runtime)
         else()
            install(FILES ${QTROOT}/plugins/imageformats/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/imageformats" COMPONENT Runtime)
         endif()
      endif()
   endforeach()
   
   foreach(DLL qminimal;qoffscreen;qwindows;qlinuxfb;qxcb)
      if(EXISTS ${QTROOT}/plugins/platforms/${FILEPREFIX}${DLL}${FILESUFFIX})
         if(WIN32)
            install(FILES ${QTROOT}/plugins/platforms/${FILEPREFIX}${DLL}d${FILESUFFIX} DESTINATION "${OUTDIR}/platforms" CONFIGURATIONS Debug COMPONENT Runtime)
            install(FILES ${QTROOT}/plugins/platforms/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/platforms" CONFIGURATIONS Release RelWithDebInfo COMPONENT Runtime)
         else()
            install(FILES ${QTROOT}/plugins/platforms/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/platforms" COMPONENT Runtime)
         endif()
      else()
      endif()
   endforeach()

   if(WIN32)
      foreach(DLL windowsprintersupport)
         if(EXISTS ${QTROOT}/plugins/printsupport/${FILEPREFIX}${DLL}${FILESUFFIX})
            install(FILES ${QTROOT}/plugins/printsupport/${FILEPREFIX}${DLL}d${FILESUFFIX} DESTINATION "${OUTDIR}/printsupport" CONFIGURATIONS Debug COMPONENT Runtime)
            install(FILES ${QTROOT}/plugins/printsupport/${FILEPREFIX}${DLL}${FILESUFFIX} DESTINATION "${OUTDIR}/printsupport" CONFIGURATIONS Release RelWithDebInfo COMPONENT Runtime)
         endif()
      endforeach()
   endif()

   # Needed for Linux
   if(NOT WIN32)
      install(DIRECTORY ${QTROOT}/plugins/ DESTINATION "${OUTDIR}" COMPONENT Runtime)
   endif()
endmacro()

macro(install_qt_licenses)
   file(GLOB LICENSES ${QTROOT}/licenses/*.txt)
   install(FILES ${LICENSES} DESTINATION ${INSTALL_EXE_PATH}/../resources/data/3rd_party_licenses PERMISSIONS
           OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE COMPONENT Resource)
endmacro()

# These are included backwards, so that /lib shows up first.  This way the .dll's found are next to the .pdb files
set_property(GLOBAL PROPERTY qt_BINDIR_DEBUG ${QTROOT}/bin ${QTROOT}/lib)
set_property(GLOBAL PROPERTY qt_BINDIR_RELEASE ${QTROOT}/bin ${QTROOT}/lib)
